/**
 * @Author: 陈瑞鹏
 * @Date:   2022-10-18 17:15:37
 * @Last Modified by:   陈瑞鹏
 * @Last Modified time: 2022-11-10 15:38:10
 */
import express from 'express';
import { heroApi } from '../config/api.config';
import item from '../controllers/item.controller';
import reward from '../controllers/reward.controller';
import user from '../controllers/user.contorller';
import { Code } from '../enum/code.enum';
import { FUNC_NAME, HANDLE_TYPE, PROP_NAME } from '../enum/itemLogs.enum';
import Hero from '../models/hero/hero.model';
import HeroFrag from '../models/hero/heroFrag.model';
import Result from '../utils/result';
import utils from '../utils/utils';
const heroRoute = express.Router();

/**
* @api {post} /hero/upHero 升级武将
* @apiGroup 武将
* @apiParam {Number} roleId 角色ID
* @apiParam {Number} id 武将ID
* @apiParam {Number} type 类型  1=一级  2=十级
* @apiName upgrade
* @apiVersion 1.0.0
*/
heroRoute.post(heroApi.upgrade, async (req, res) => {
  let { roleId, id, type } = req.body;

  let userInfo: any = await user.getUserInfo(roleId);

  let HeroRes: any = await Hero.findOne({ roleId, id });
  if (!HeroRes) throw new Result(Code.Fail, '未拥有该武将');

  let heroData: any = await utils.get_id_json('hero/hero.json', id, 'id');

  let lv = 0;
  let upCond: any = await utils.get_id_json('hero/upgradeCost.json', HeroRes.level, 'lv');

  let cost = Math.round(upCond.cost * heroData.costRa);
  let desc = '';
  switch (Number(type)) {
    case 1:
      lv = HeroRes.level + 1;
      desc = `[${HeroRes.name}] ${HeroRes.level}级=>${lv}级`;
      if (lv - userInfo.roleLv > 10) throw new Result(Code.Fail, '武将等级不能超出主公等级10级');
      let ret = await reward.reduceGoldCoin(roleId, cost, desc, FUNC_NAME.UP_HERO);
      if (ret == -1) throw new Result(Code.Fail, '金币不足');
      break;
    case 2:
      lv = HeroRes.level + 10;
      desc = `[${HeroRes.name}] ${HeroRes.level}级=>${lv}级`;
      let num = 0;
      for (var i = HeroRes.level; i < lv; i++) {
        let upCond: any = await utils.get_id_json('hero/upgradeCost.json', i, 'lv');
        let cost = Math.round(upCond.cost * heroData.costRa);
        num += cost;
      }
      ret = await reward.reduceGoldCoin(roleId, num, desc, FUNC_NAME.UP_HERO);
      if (ret == -1) throw new Result(Code.Fail, '金币不足');
      break;
  }

  if (!lv) {

    throw new Result(Code.Fail, '升级失败,类型错误');
  }

  await Hero.updateOne({ roleId, id }, { level: lv });

  userInfo = await user.getUserInfo(roleId);

  let heroRes = await Hero.find({ roleId });
  let retData = {
    userInfo,
    heroRes
  }

  return res.json({ code: Code.Succ, message: "升级成功!", data: retData });
})

/**
* @api {post} /hero/heroAdvanced 武将进阶
* @apiGroup 武将
* @apiParam {Number} roleId 角色ID
* @apiParam {Number} id 武将ID
* @apiName heroAdvanced
* @apiVersion 1.0.0
*/
heroRoute.post(heroApi.heroAdvanced, async (req, res) => {
  let { roleId, id } = req.body;

  let HeroRes: any = await Hero.findOne({ roleId, id });
  if (!HeroRes) throw new Result(Code.Fail, '未拥有该武将');

  let advancedLv = HeroRes.stepLv + 1;

  let heroFrag = advancedLv * 2 + 10;

  let HeroFragRes: any = await HeroFrag.findOne({ roleId, id });
  if (!HeroFragRes) throw new Result(Code.Fail, '碎片不足');

  if (HeroFragRes.num - heroFrag < 0) throw new Result(Code.Fail, '碎片不足');

  await HeroFrag.updateOne({ roleId, id }, { num: HeroFragRes.num - heroFrag });

  await item.itemLogs(roleId, HANDLE_TYPE.REDUCE, PROP_NAME.HERO_FRAG, heroFrag, `[${HeroRes.name}] ${HeroRes.stepLv}阶=>${advancedLv}`, HeroFragRes.num, HeroFragRes.num - heroFrag, FUNC_NAME.HERO_ADVANCED);

  await Hero.updateOne({ roleId, id }, { stepLv: advancedLv });

  await item.itemLogs(roleId, HANDLE_TYPE.UPDATE, PROP_NAME.ADVANCED_LV, 1, `[${HeroRes.name}] ${HeroRes.stepLv}阶=>${advancedLv}`, HeroRes.stepLv, advancedLv, FUNC_NAME.HERO_ADVANCED)


  let userInfo: any = await user.getUserInfo(roleId);
  HeroFragRes = await HeroFrag.find({ roleId });
  let retData = {
    userInfo,
    HeroFragRes
  }

  return res.json({ code: Code.Succ, message: "进阶成功!", data: retData });
})

/**
 * 英雄属性点分配
 */
heroRoute.post(heroApi.heroAttr, async (req, res) => {
  let { roleId, id, extraBonus } = req.body;
  extraBonus = extraBonus.split(",");
  let HeroRes: any = await Hero.findOne({ roleId, id });
  if (!HeroRes) throw new Result(Code.Fail, '未拥有该武将');
  await Hero.updateOne({ roleId, id }, { extraBonus });
  await item.itemLogs(roleId, HANDLE_TYPE.UPDATE, PROP_NAME.ATTR_CHANGE, 1, `${JSON.stringify(HeroRes.extraBonus)}=>${JSON.stringify(extraBonus)}`, JSON.stringify(HeroRes.extraBonus), JSON.stringify(extraBonus), FUNC_NAME.ATTR_CHANGE)
  HeroRes.extraBonus = extraBonus;
  let retData = {
    HeroRes
  }
  return res.json({ code: Code.Succ, message: "修改成功!", data: retData });
})

export default heroRoute;